package com.tongna.controller;

import com.tongna.domain.News;
import com.tongna.service.NewsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * DubboDemo
 *
 * @author 张浩伟
 * @version 1.01 2018年02月01日
 */
@Controller
@RequestMapping("news")
public class NewsController {

    private static Logger log = LoggerFactory.getLogger(NewsController.class);

    @Autowired
    private NewsService service;

    @GetMapping("add")
    public String add(){
        return "news/add";
    }

    @PostMapping("add")
    public String add(News news, Model model){
        service.add(news);

        return "redirect:/news/list";
    }

    @GetMapping("{id}")
    public String add(@PathVariable("id") Long id, Model model){
        model.addAttribute("model", service.findOne(id));
        return "/news/detail";
    }

    @GetMapping("list")
    public String list(Model model){
        // TODO: 2018/2/3 业务代码

        model.addAttribute("list", service.findAll());
        return "news/list";
    }

}
